<?php
##############################################
#
#   预约列表
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\MakeModel;


class MakeController extends AdminBaseController {
	
	protected $_make;
	
	public function _initialize(){
		parent::_initialize();
		$this->_make = new MakeModel();
	}
	
	/**
	 * 预约列表
	 */
	public function makeList(){
	    $add_time = I('add_time');
	    if($add_time){
	        $gap = explode(' - ', $add_time);
	        $start_time = strtotime($gap[0]);
	        $end_time = strtotime($gap[1]);
	    }
	    $data = $this->_make->getMake(I('name'), I('mobile'), I('type'), $start_time, $end_time);
            $category = M('apply_category')->order('sort asc')->select();
	    $this -> assign('make', $data[0]);
	    $this -> assign('page', $data[1]);
            $this -> assign('category', $category);
            $this -> assign('sourch', array('name'=>I('name'),'type'=>I('type'),'mobile'=>I('mobile'),'add_time'=>$add_time));
	    $this -> display('index');
	}
	
	/**
	 * 添加活动
	 */
 	public function addMake(){
		 if(IS_GET){
		    $id = I('id');
		    $act = I('get.act');
		    if($id && $act == 'edit'){
		        $data = $this->_make->getMakeFind($id);
                        $type_name = M('apply_category')->where('id='.$data['type'])->field('name')->find();
                        $data['type_name'] = $type_name['name'];
                        $data['select_time'] = explode(',',$data['select_time']);
		        $this->assign('id', $id);
		        $this->assign('make', $data);
		    }
		    $this->assign('act', $act);
		    $this->display('add');
		}else if(IS_POST){
		    $data = I('post.');
                    $data['select_time'] = implode(',',$data['select_time']);
		   	$act = I('post.act');
		   	if($act == 'edit'){
                            $arr = array(
                                'name'=>$data['name'],
                                'mobile'=>$data['mobile'],
                                'select_time'=>$data['select_time'],
                                'status'=>$data['status'],
                                'trial_status'=>$data['trial_status'],
                                'reason'=>$data['reason'],
                            );
                            //发送给用户消息
                            self::sendMessage($data['id'], $data['user_id'], $data['trial_status'], '预约申请', $data['reason']);
                            $result = M('make')->where('id='.$data['id'])->save($arr);
		   	    if($result)
		   	        $this -> success('修改成功', U('makeList'), 2);
		   	    else
		   	        $this -> error('操作失败', '', 2);
		   	} 
    	}
	}

        
	/**
	 * 全选删除订单
	 */
    public function deleteAllMake(){
	    if(!IS_POST) return;
	    $result = $this->_make->deleteAllMake($_POST);
	    if($result)
			$this -> success('删除成功', U('orderList'), 2);
		else
			$this -> error('操作失败', '', 2);
	}
	
	/**
	 * 删除预约
	 */
	public function deleteMake(){
		if(!IS_AJAX) return;
                $id = I('id');
                $count = M('make_users')->where('make_id='.$id)->count();
                if($count){
                    $this->ajaxReturn(array('status' => 2,'message' => '有客户参与该预约不能删除'));
                }
		$result = $this->_make->deleteMake(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.make_id');
	    if(!in_array($operate,array('trial_status','not_trial_status','is_top','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
            $user_ids = M('make')->where($map)->field('user_id,id')->select();
	    $make = M('make');
	    switch ($operate){
	        case 'del':
	            $row = $make->where($map)->delete();
	            break;
	        case 'trial_status':
	            $data = array('trial_status'=>2);
                    //发送给用户消息
                    foreach ($user_ids as $k=>$v){
                        self::sendMessage($v['id'], $v['user_id'], 2, '预约申请', '');
                    }
	            $row = $make->where($map)->save($data);
	            break;
	        case 'not_trial_status':
	            $data = array('trial_status'=>3);
                    //发送给用户消息
                    foreach ($user_ids as $k=>$v){
                        self::sendMessage($v['id'], $v['user_id'], 3, '预约申请', '');
                    }
	            $row = $make->where($map)->save($data);
	            break;
	        case 'is_top':
	            $data = array('is_top'=>1);
	            $row = $make->where($map)->save($data);
	    }
	    if($row !== false)
	        $this->success('操作成功', U('makeList'), 1);
	    else
	        $this->error('操作失败');
	}
        
        //预约订单列表
        public function orderForm(){
            if(IS_GET){
                $data = I('get.');
//                var_dump($data);
                if($data['add_time']){
                    $gap = explode(' - ', $data['add_time']);
                    $start_time = strtotime($gap[0]);
                    $end_time = strtotime($gap[1]);
                    if($start_time && $end_time){
                        $map['add_time'] = array(array('egt', $start_time),array('elt', $end_time)) ;
                    }
                }
                if($data['order_status']){
                    $map['order_status'] = $data['order_status'];
                }
                if($data['pay']){
                    $map['pay'] = $data['pay'];
                }
                if($data['position']){
                    $map['position'] = array('like','%'.$data['position'].'%');
                }
            }
            $count = M('make_users')->alias('m')->field('m.*,k.name,k.position')->join('__MAKE__ AS k ON k.id=m.make_id')->where($map)->count();
            $Page = new \Think\Page($count,10);
            $show = $Page->show();
            $orderList = M('make_users')
                    ->alias('m')
                    ->field('m.*,k.name,k.position')   
                    ->join('__MAKE__ AS k ON k.id=m.make_id')
                    ->where($map)
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->select();
            $this->assign('orderList',$orderList);
            $this->assign('page',$show);
            $this->assign('data',$data);
            $this->display();
        }
        
        /**
	 * 添加订单
	 */
        public function addOrder() {
        if (IS_GET) {
            $id = I('id');
            $act = I('get.act');
            if ($id && $act == 'edit') {
                $data = M('make_users')->alias('m')->field('m.*,k.name,k.position')->join('__MAKE__ AS k ON k.id=m.make_id')->where('m.id='.$id)->find();
                $this->assign('id', $id);
                $this->assign('order', $data);
            }
            $this->assign('act', $act);
            $this->display();
        } else if (IS_POST) {
            $data = I('post.');
            var_dump($data);die;
            $data = array(
                
            );
            $admin_base_model = new AdminBaseModel('activity_order');
            $act = I('post.act');
            if ($act == 'add') {
                $result = $admin_base_model->addData($data);
                if ($result)
                    $this->success('添加成功', U('orderList'), 1);
                else
                    $this->error('操作失败', '', 1);
            }elseif ($act == 'edit') {
                $id = I('id');
                if ($this->_order->editOrder($id, $data) !== false)
                    $this->success('修改成功', U('orderList'), 1);
                else
                    $this->error('操作失败', '', 1);
            }
        }
    }
    
    /**
	 * 发送专家消息
	 */
	public function sendMessage($make_id, $user_id, $status, $name, $reason){
	    
	    switch ($status) {
	        case 2:
	        $data = array(
                    'make_id'=>$make_id,
                    'user_id'=>$user_id,
	            'title'=>'预约审核通过', 
	            'content'=>'恭喜你，你的 "'.$name.'" 已经审核通过',
	            'auth'=>'admin',
                    'add_time'=>time());
	        M('make_message')->add($data);
	        break;
	        case 3:
                    $data = array(
                    'make_id' => $make_id,
                    'user_id' => $user_id,
                    'title' => '预约审核不予通过',
                    'content' => '抱歉，你的  "' . $name . '" 审核不予通过。<br/><p style="color:red;">原因：' . $reason . '</p>',
                    'auth' => 'admin',
                    'add_time' => time());
                M('make_message')->add($data);
	        break;
	    }
	    return true;
	}
    

}



	
	